@php
    $user_id = Auth::user()->id;
@endphp
@extends('layouts.default')

@section('title', '公开教学')

@push('scripts')
<script src="{{ asset('lib/xm-select-v1.2.4/xm-select.js')}}"></script>
<script>
    layui.use(['layer','form','table','laytpl','laydate','element','laypage','upload'], function(){
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var element = layui.element;
        var laypage = layui.laypage;
        var upload = layui.upload;
        var xmDemo;
        var zmrDemo;
        var fzrDemo;
        var zmdFiles = [];
        var jaFiles = [];
        var kjFiles = [];

        renderSelect();

        laydate.render({
            elem: '#begin-date'
            ,type: 'date'
        });

        //下拉框初始化
        async function renderSelect(){
            await renderDictionarySelect();
            await renderTermSelect();
            await renderParticipants();
            xmDemo.setValue(['{{$user_id}}']);
        }

        //字典下拉框数据渲染
        function renderDictionarySelect() {
            return new Promise((resolve) => {
                getDictionaryItem({'diccode': ['LEVEL', 'SUBJECT', 'GRADE', 'reception_class', 'class']}, function (res){
                    if(res.code === 1) {
                        let data = res.data;

                        for(var k in data) {
                            let key = k.toLowerCase();
                            let d = data[k];

                            $('[data-role='+key+'List]').empty().append('<option value="">请选择</option>');
                            for (var i in d) {
                                $('[data-role='+key+'List]').append('<option value="' + d[i].optionId + '">' + d[i].selectValue + '</option>');
                            }
                        }

                        form.render();

                        resolve();
                    }
                });
            });
        }

        //学期下拉框数据渲染
        function renderTermSelect() {
            return new Promise((resolve) => {
                getTermData({},function(res){
                    if(res.code == 1){
                        $('[data-role=termList]').empty().append('<option value="">请选择</option>');
                        for(var i in res.data){
                            $('[data-role=termList]').append('<option value="'+res.data[i].id+'">'+res.data[i].title+'</option>');
                        }

                        form.render('select');

                        resolve();
                    }
                });
            });
        }

        //参与教师下拉框数据渲染
        function renderParticipants() {
            return new Promise((resolve) => {
                getParticipantsData({},function(res){
                    if(res.code == 1){
                        xmDemo = xmSelect.render({
                            el: '#pter',
                            autoRow: true,
                            filterable: true,
                            tree: {
                                show: true,
                                showFolderIcon: true,
                                showLine: true,
                                indent: 20,
                                expandedKeys:false
                            },
                            direction: 'down',
                            filterable: true,
                            height: 'auto',
                            name:'participant',
                            layVerify: 'required',
                            layVerType: 'msg',
                            layReqText:'参与教师不能为空',
                            theme: {
                                color: '#2d8cf0',
                            },
                            //disabled:true,
                            searchTips:'关键字搜索',
                            data: function(){
                                return res.data
                            }
                        })

                        return resolve();
                    }
                });
            });
        }
        //证明人下拉框数据渲染
        function renderZmrSelector() {
            getParticipantsData({},function(res){
                if(res.code == 1){
                    zmrDemo = xmSelect.render({
                        el: '#zmr-xms',
                        autoRow: true,
                        filterable: true,
                        tree: {
                            show: true,
                            showFolderIcon: true,
                            showLine: true,
                            indent: 20,
                            expandedKeys:false
                        },
                        direction: 'down',
                        filterable: true,
                        height: 'auto',
                        name:'zmr',
                        // layVerify: 'required',
                        // layVerType: 'msg',
                        // layReqText:'证明人不能为空',
                        theme: {
                            color: '#2d8cf0',
                        },
                        searchTips:'关键字搜索',
                        data: function(){
                            return res.data
                        }
                    });
                }
            });
        }
        //证明人部门负责人下拉框数据渲染
        function renderFzrSelector() {
            getParticipantsData({},function(res){
                if(res.code == 1){
                    fzrDemo = xmSelect.render({
                        el: '#fzr-xms',
                        autoRow: true,
                        filterable: true,
                        tree: {
                            show: true,
                            showFolderIcon: true,
                            showLine: true,
                            indent: 20,
                            expandedKeys:false
                        },
                        direction: 'down',
                        filterable: true,
                        height: 'auto',
                        name:'fzr',
                        // layVerify: 'required',
                        // layVerType: 'msg',
                        // layReqText:'证明人部门负责人不能为空',
                        theme: {
                            color: '#2d8cf0',
                        },
                        searchTips:'关键字搜索',
                        data: function(){
                            return res.data
                        }
                    });
                }
            });
        }

        window.fileDelete = function (fk, ft) {
            if(ft == 1) {
                delete zmdFiles[fk];
                let files = zmdFiles;

                $('#zmd-file').val(JSON.stringify(zmdFiles));
                $('#zmd-uploader').parent().find('.file-show').html('');
                for(i in files) {
                    if(files[i]) {
                        let fsHtml = '<div class="file-item"><span class="file-name">'+files[i].file_name+'</span><span class="layui-icon layui-icon-close-fill" onClick="fileDelete('+i+', 1)"></span></div>';
                        $('#zmd-uploader').parent().find('.file-show').append(fsHtml);
                    }
                }
            } else if(ft == 2) {
                delete jaFiles[fk];
                let files = jaFiles;

                $('#ja-file').val(JSON.stringify(jaFiles));
                $('#ja-uploader').parent().find('.file-show').html('');
                for(i in jaFiles) {
                    if(files[i]) {
                        let fsHtml = '<div class="file-item"><span class="file-name">'+files[i].file_name+'</span><span class="layui-icon layui-icon-close-fill" onClick="fileDelete('+i+', 2)"></span></div>';
                        $('#ja-uploader').parent().find('.file-show').append(fsHtml);
                    }
                }
            } else if(ft == 3) {
                delete kjFiles[fk];
                let files = kjFiles;

                $('#kj-file').val(JSON.stringify(kjFiles));
                $('#kj-uploader').parent().find('.file-show').html('');
                for(i in kjFiles) {
                    if(!files[i]) return;

                    let fsHtml = '<div class="file-item"><span class="file-name">'+files[i].file_name+'</span><span class="layui-icon layui-icon-close-fill" onClick="fileDelete('+i+', 3)"></span></div>';
                    $('#kj-uploader').parent().find('.file-show').append(fsHtml);
                }
            }
        }

        //文件上传
        upload.render({
            elem: '#zmd-uploader'
            ,url: "{{route('uploads.upload')}}" //此处配置你自己的上传接口即可
            ,accept: 'file' //普通文件
            ,multiple: true
            ,size: 102400
            ,choose: function(obj){
                //上传loading
                showloading(true);
                obj.preview(function(index, file, result){
                    if(file.size > 102400) {
                        showloading(false, 1);
                    }
                });
            }
            ,allDone: function(obj){ //当文件全部被提交后，才触发
                showloading(false);
                if(obj.total == obj.successful) {

                } else {
                    layer.msg(obj.failed + '个文件上传失败！');
                }
            }
            ,done: function(res){
                if(res.code != 1) {
                    layer.msg(res.msg);
                    return false;
                }
                zmdFiles.push(res.data);
                $('#zmd-file').val(JSON.stringify(zmdFiles));

                $('#zmd-uploader').parent().find('.file-show').html('');
                for(i in zmdFiles) {
                    if(zmdFiles[i]) {
                        let fsHtml = '<div class="file-item"><span class="file-name">'+zmdFiles[i].file_name+'</span><span class="layui-icon layui-icon-close-fill" onClick="fileDelete('+i+', 1)"></span></div>';
                        $('#zmd-uploader').parent().find('.file-show').append(fsHtml);
                    }
                }
                layer.msg('上传成功');
            }
            ,error: function(index, upload){ //错误回调
                console.log(index);
            }
        });
        upload.render({
            elem: '#ja-uploader'
            ,url: "{{route('uploads.upload')}}" //此处配置你自己的上传接口即可
            ,accept: 'file' //普通文件
            ,multiple: true
            ,size: 102400
            ,choose: function(obj){
                //上传loading
                showloading(true);
                obj.preview(function(index, file, result){
                    if(file.size > 102400) {
                        showloading(false, 1);
                    }
                });
            }
            ,allDone: function(obj){ //当文件全部被提交后，才触发
                showloading(false);
                if(obj.total == obj.successful) {

                } else {
                    layer.msg(obj.failed + '个文件上传失败！');
                }
            }
            ,done: function(res){
                if(res.code != 1) {
                    layer.msg(res.msg);
                    return false;
                }
                jaFiles.push(res.data);
                $('#ja-file').val(JSON.stringify(jaFiles));

                $('#ja-uploader').parent().find('.file-show').html('');
                for(i in jaFiles) {
                    if(jaFiles[i]) {
                        let fsHtml = '<div class="file-item"><span class="file-name">'+jaFiles[i].file_name+'</span><span class="layui-icon layui-icon-close-fill" onClick="fileDelete('+i+', 2)"></span></div>';
                        $('#ja-uploader').parent().find('.file-show').append(fsHtml);
                    }
                }
                layer.msg('上传成功');
            }
            ,error: function(index, upload){ //错误回调
                console.log(index);
            }
        });
        upload.render({
            elem: '#kj-uploader'
            ,url: "{{route('uploads.upload')}}" //此处配置你自己的上传接口即可
            ,accept: 'file' //普通文件
            ,multiple: true
            ,size: 102400
            ,choose: function(obj){
                //上传loading
                showloading(true);
                obj.preview(function(index, file, result){
                    if(file.size > 102400) {
                        showloading(false, 1);
                    }
                });
            }
            ,allDone: function(obj){ //当文件全部被提交后，才触发
                showloading(false);
                if(obj.total == obj.successful) {

                } else {
                    layer.msg(obj.failed + '个文件上传失败！');
                }
            }
            ,done: function(res){
                if(res.code != 1) {
                    layer.msg(res.msg);
                    return false;
                }
                kjFiles.push(res.data);
                $('#kj-file').val(JSON.stringify(kjFiles));

                $('#kj-uploader').parent().find('.file-show').html('');
                for(i in kjFiles) {
                    if(kjFiles[i]) {
                        let fsHtml = '<div class="file-item"><span class="file-name">'+kjFiles[i].file_name+'</span><span class="layui-icon layui-icon-close-fill" onClick="fileDelete('+i+', 3)"></span></div>';
                        $('#kj-uploader').parent().find('.file-show').append(fsHtml);
                    }
                }
                layer.msg('上传成功');
            }
            ,error: function(index, upload){ //错误回调
                console.log(index, 1);
            }
        });

        form.on('select(levelList)', function(data){
            zmdFiles = [];
            $("#zmd-file").val('');
            $('#zmd-generate').parent().find('.file-show').html('');

            levelChange(data.value);
        });
        function levelChange(v) {
            if(v >= 111 && v != 850) {
                $(".zmd-item").removeClass('layui-hide');

                $("#zmd-uploader").addClass('layui-hide');
                $("#zmd-generate").removeClass('layui-hide');
                $("#zmrzw").val('教研组长');

                renderZmrSelector();
                renderFzrSelector();
            } else {
                $(".zmd-item").addClass('layui-hide');

                $("#zmd-uploader").removeClass('layui-hide');
                $("#zmd-generate").addClass('layui-hide');
                $("#zmrzw").val('');
            }

            form.render();
        }
        form.on('select(zmStyleList)', function(data){
            if(data.value == 1) {
                $(".gkk-category").removeClass('layui-hide');
            } else {
                $(".gkk-category").addClass('layui-hide');
            }
        });


        $(document).on('click','#zmd-generate',function(){

            form.submit('addForm', function(data){
                var field = data.field; // 获取表单全部字段值
                console.log(data); // 回调函数返回的 data 参数和提交事件中返回的一致

                field.zmr_ids = zmrDemo.getValue('value');
                field.fzr_ids = fzrDemo.getValue('value');

                if(field.zmr_ids.length <= 0){
                    layer.msg('证明人不能为空',{icon: 5,time: 1000});
                    return false;
                }
                if(field.fzr_ids.length <= 0){
                    layer.msg('证明人部门负责人不能为空',{icon: 5,time: 1000});
                    return false;
                }
                if(!field.witness_position){
                    layer.msg('证明人职务不能为空',{icon: 5,time: 1000});
                    return false;
                }
                // if(!field.zm_style){
                //     layer.msg('形式不能为空',{icon: 5,time: 1000});
                //     return false;
                // }
                // if(field.zm_style == 1 && !field.zm_gkk_category){
                //     layer.msg('公开课类别不能为空',{icon: 5,time: 1000});
                //     return false;
                // }
                if(!field.place){
                    layer.msg('地点不能为空',{icon: 5,time: 1000});
                    return false;
                }
                if(!field.target){
                    layer.msg('参与对象不能为空',{icon: 5,time: 1000});
                    return false;
                }

                generateCertificate(field, function(res){
                    if(res.code == '1'){
                        layer.msg(res.msg,{icon: 6,time: 1000},function(){
                            zmdFiles = res.data;
                            let files = zmdFiles;
                            $('#zmd-file').val(JSON.stringify(zmdFiles));

                            $('#zmd-generate').parent().find('.file-show').html('');
                            for(i in files) {
                                if(files[i]) {
                                    let fsHtml = '<div class="file-item"><span class="file-name">'+files[i].file_name+'</span><span class="layui-icon layui-icon-close-fill" onClick="fileDelete('+i+', 1)"></span></div>';
                                    $('#zmd-generate').parent().find('.file-show').append(fsHtml);
                                }
                            }
                        });
                    }else{
                        layer.msg(res.msg, {icon: 5,shift : 6});
                    }
                });
            });
        });

        //提交
        form.on('submit(sub)', function(data){
            let d = data.field;
            d.participants = xmDemo.getValue('nameStr');
            d.participant_ids = xmDemo.getValue('value');
            d.status = 1;
            d.do_task = 1;

            if(!$("#zmd-file").val()) {
                layer.msg('请上传公开课证明！', {icon: 5,time: 1000});
                return false;
            }
            if(!$("#ja-file").val()) {
                layer.msg('请上传教案文件！', {icon: 5,time: 1000});
                return false;
            }
            if(!$("#kj-file").val()) {
                layer.msg('请上传课件文件！', {icon: 5,time: 1000});
                return false;
            }

            if(!d.activity_category) {
                layer.msg('请选择类别！', {icon: 5,shift : 6});
                return false;
            }

            if(d.activity_category == 'TEARRESEARCH_OPEN_TABLE') {
                if(!d.course){
                    layer.msg('请选择学科！', {icon: 5,shift : 6});
                    return false;
                }
            }

            addActivity(d, function(res){
                if(res.code == '1'){
                    layer.msg(res.msg,{icon: 6,time: 1000},function(){
                        window.history.back();
                    });
                }else{
                    layer.msg(res.msg, {icon: 5,shift : 6});
                }
            });

            return false;
        });

        //保存
        $('#save-btn').click(function(){
            var isValid = form.validate('.data-version-id');
            if(!isValid) return false;

            var data = form.val("addForm");
            data.participants = xmDemo.getValue('nameStr');
            data.participant_ids = xmDemo.getValue('value');
            data.status = 2;

            addActivity(data,function(res){
                if(res.code == '1'){
                    layer.msg(res.msg,{icon: 6,time: 1000},function(){
                        window.history.back();
                    });
                }else{
                    layer.msg(res.msg, {icon: 5,shift : 6});
                }
            });
            return false;
        });

        form.on('radio(activityCategoryR)', function(data){
            console.log(data.elem); //得到radio原始DOM对象
            console.log(data.value); //被点击的radio的value值

            if(data.value == 'TEARRESEARCH_OPEN_TABLE') {
                $(".subject-redstar").removeClass('layui-hide');
            } else {
                $(".subject-redstar").addClass('layui-hide');
            }

        });
    });
</script>
@endpush

@section('content')
<style>
    .opt-panel{padding: 10px;}
    .panel-title h3{margin-bottom: 20px;font-weight: 800;}

    .opt-form{padding: 20px 5px;}
    .opt-area{display: flex;flex-direction: row-reverse;padding: 10px 20px;margin-bottom: 20px;}
    .opt-area button, .back-btn{margin-left: 10px;}
    .layuik-upload{position: relative;}
    .redtip{position: absolute;top: 38px;}
</style>

<div class="layui-row">
    <div class="layui-panel opt-panel">
        <form class="layui-form opt-form" action="" id="add-form" lay-filter="addForm">
            <div class="layui-row">
                <div class="panel-title">
                    <h3>公开教学 - 新增</h3>
                </div>
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>学年学期：</label>
                        <div class="layuik-input-inline">
                            <select class="data-version-id" name="data_version_id" lay-filter="termList" data-role="termList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>课题名称：</label>
                        <div class="layuik-input-inline">
                            <input type="text" name="name" required lay-verify="required" placeholder="请输入" lay-reqText="不能为空" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>日期：</label>
                        <div class="layuik-input-inline">
                            <input type="text" name="begin_date" id="begin-date" required lay-verify="required" placeholder="请选择" lay-reqText="不能为空" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>级别：</label>
                        <div class="layuik-input-inline">
                            <select name="level" lay-filter="levelList" data-role="levelList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>类别：</label>
                        <div class="layuik-input-inline">
                            <input type="radio" name="activity_category" lay-filter="activityCategoryR" value="EW_CLASS_MEETING_TABLE" title="教育" class="layui-radio">
                            <input type="radio" name="activity_category" lay-filter="activityCategoryR" value="TEARRESEARCH_OPEN_TABLE" title="教学" class="layui-radio">
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar subject-redstar">*</i>学科：</label>
                        <div class="layuik-input-inline">
                            <select name="course" lay-filter="subjectList" data-role="subjectList" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>年级：</label>
                        <div class="layuik-input-inline">
                            <select name="grade" lay-filter="gradeList" data-role="gradeList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>班级：</label>
                        <div class="layuik-input-inline">
                            <select name="class_item" lay-filter="classList" data-role="classList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>研究课：</label>
                        <div class="layuik-input-inline">
                            <select name="reception_class" lay-filter="reception_classList" data-role="reception_classList" lay-verify="required" lay-reqText="不能为空"></select>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item layuik-columns-two zmd-item layui-hide">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>地点：</label>
                        <div class="layuik-input-inline">
                            <input type="text" name="place" placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>参与对象：</label>
                        <div class="layuik-input-inline">
                            <input type="text" name="target" placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item zmd-item layui-hide">
                    <div class="layuik-inline">
                        <label class="layuik-form-label">备注：</label>
                        <div class="layuik-input-inline">
                            <textarea name="zmd_remark" id="" rows="3" class="layui-textarea" placeholder="请输入"></textarea>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>参与教师：</label>
                        <div class="layuik-input-inline">
                            <div type="text" id="pter" class="xm-select-demo"></div>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item">
                    <div class="layuik-inline">
                        <label class="layuik-form-label">说明：</label>
                        <div class="layuik-input-inline">
                            <textarea name="remark" id="" rows="3" class="layui-textarea" placeholder="请输入"></textarea>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item layuik-columns-two zmd-item layui-hide">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>证明人：</label>
                        <div class="layuik-input-inline">
                            <div type="text" id="zmr-xms" class="xm-select-demo"></div>
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>部门负责人：</label>
                        <div class="layuik-input-inline">
                            <div type="text" id="fzr-xms" class="xm-select-demo"></div>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item layuik-columns-two zmd-item layui-hide">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>证明人职务：</label>
                        <div class="layuik-input-inline">
                            <input type="text" name="witness_position" id="zmrzw" placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>

                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>公开课证明：</label>
                        <div class="layuik-input-inline layuik-upload zmd-upload-area">
                            <button type="button" class="layui-btn layui-btn-normal" id="zmd-uploader"><i class="layui-icon"></i>上传文件</button>
                            <button type="button" class="layui-btn layui-btn-normal layui-hide" id="zmd-generate">生成文件</button>
                            <div class="file-show"></div>
                            <input style="display:none" type="text" id="zmd-file" name="files[zmd]" value="">
                            <p class="redtip">上传文件不超过100M</p>
                        </div>
                    </div>
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>教案：</label>
                        <div class="layuik-input-inline layuik-upload">
                            <button type="button" class="layui-btn layui-btn-normal" id="ja-uploader"><i class="layui-icon"></i>上传文件</button>
                            <div class="file-show"></div>
                            <input style="display:none" type="text" id="ja-file" name="files[ja]" value="">
                            <p class="redtip">上传文件不超过100M</p>
                        </div>
                    </div>
                </div>
                <div class="layuik-form-item layuik-columns-two">
                    <div class="layuik-inline">
                        <label class="layuik-form-label"><i class="redstar">*</i>课件：</label>
                        <div class="layuik-input-inline layuik-upload">
                            <button type="button" class="layui-btn layui-btn-normal" id="kj-uploader"><i class="layui-icon"></i>上传文件</button>
                            <div class="file-show"></div>
                            <input style="display:none" type="text" id="kj-file" name="files[kj]" value="">
                            <p class="redtip">上传文件不超过100M</p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-row">
                <div class="opt-area">
                    <a href="javascript:history.go(-1);" class="layui-btn layui-btn-primary back-btn">返回</a>
                    <button class="layui-btn layui-btn-primary" type="button" id="save-btn" style="color:#2d8cf0;">保存</button>
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="sub">提交</button>
                </div>
            </div>
        </form>
    </div>
</div>

@endsection
